package kr.co.exight.onApp;

import library.Library;
import library.Library.DialogChoiceYes;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class ActivityWebView extends Activity implements OnClickListener {

	/** Called when the activity is first created. */

	WebView wv;
	ProgressBar pb;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_webview);
		// TODO Auto-generated method stub

		wv = (WebView)findViewById(R.id.webView_wvMain);

		findViewById(R.id.webView_btnLeft).setOnClickListener(this);
		findViewById(R.id.webView_btnRefresh).setOnClickListener(this);
		findViewById(R.id.webView_btnRight).setOnClickListener(this);
		pb = (ProgressBar)findViewById(R.id.webView_progressBar);
		pb.setVisibility(View.GONE);

		
		String url = null;
		int naviImage = 0;
		switch(getIntent().getIntExtra("KEY", 0)){
		case 0:
			Library.makeYesDialog(this, "인터넷 연결이 좋지 않습니다.\n연결을 확인해주세요", "확인", new DialogChoiceYes() {

				public boolean choiceYes() {
					// TODO Auto-generated method stub
					onBackPressed();
					return false;
				}
			});
			break;
		case 1:
			url ="http://focuson.yonsei.ac.kr/xe/notice"; // 공지게시판
			naviImage = R.drawable.navi_notice;
			break;
		case 2:
			url ="http://facebook.com/focuson49";
			naviImage = R.drawable.navi_facebook;
			break;
		case 6:
			url ="http://focuson.yonsei.ac.kr/xe/promotion"; // 건의게시판
			naviImage = R.drawable.navi_ad;
			break;
		}
			
		findViewById(R.id.wv_naviBar).setBackgroundResource(naviImage);
		
		wv.loadUrl(url);
		wv.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				
				try{
					dismissDialog(0);
				}catch(IllegalArgumentException e){
					e.printStackTrace();
				}
				
				WebSettings set = wv.getSettings();
				set.setLoadWithOverviewMode(true);
				set.setUseWideViewPort(true);
				wv.setVerticalScrollbarOverlay(true);
				
				wv.setWebChromeClient(new WebChromeClient() 
				{  
					public void onProgressChanged(WebView view, int progress) 
					{  
						if (progress<100)
						{
							pb.setVisibility(ProgressBar.VISIBLE);
						}
						else if (progress==100)
						{
							pb.setVisibility(ProgressBar.GONE);
						}
						pb.setProgress(progress);  
					}   
				});
			}
		});

		showDialog(0);

	}



	@Override // 로딩바 안보이게 하기
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this, R.style.Theme_Dialog);
		View view = getLayoutInflater().inflate(R.layout.dialog_progress, null);
		dialog.setContentView(view);
		
		dialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				finish();

			}

		});
		return dialog;

	}



	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.webView_btnLeft :
			if(wv.canGoBack())
				wv.goBack();
			break;
		case R.id.webView_btnRefresh :
				wv.reload();
			break;
		case R.id.webView_btnRight :
			if(wv.canGoForward())
				wv.goForward();
			break;
		}
	}

}
